package com.gjy.mongod.util;

import java.lang.reflect.Field;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-09-18 09:16:18
 */
public class ReflectUtil {

    public static <T> T getFieldValue(T t, Field field, Class<T> fieldType) {
        try {
            Field declaredField = t.getClass().getDeclaredField(field.getName());
            Object o = declaredField.get(t);
            if (!o.getClass().equals(fieldType)) {
                throw new RuntimeException("类型不匹配");
            }
            return ((T) o);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
